package lvbu.wang.lvbuoperator.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qing.library.utils.L;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import lvbu.wang.lvbuoperator.R;
import lvbu.wang.lvbuoperator.bean.BluetoothInfoBean;
import lvbu.wang.lvbuoperator.bean.ErrorBean;

/**
 * Created by DaiQing.Zha on 2016/12/13 0013.
 */
public class ErrorListAdapter extends BaseAdapter {

    private Context context;
    private List<ErrorBean> errorBeanList;

    public ErrorListAdapter(Context context, List<ErrorBean> errorBeanList) {
        this.context = context;
        this.errorBeanList = errorBeanList;
    }

    @Override
    public int getCount() {
        return errorBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return errorBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_error, null);
            holder.tv_time = (TextView)convertView.findViewById(R.id.tv_time);
            holder.tv_error = (TextView) convertView.findViewById(R.id.tv_error);
            holder.layout = (LinearLayout) convertView.findViewById(R.id.layout);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        if (position == 0){

            holder.layout.setBackgroundColor(context.getResources().getColor(R.color.color_D2));
            holder.tv_time.setTextColor(context.getResources().getColor(R.color.red ));
            holder.tv_error.setTextColor(context.getResources().getColor(R.color.red ));
        }
        ErrorBean errorBean = errorBeanList.get(position);
        try{
            Log.e("mainVBNz","----------------------------------" + errorBeanList.size());
            int errorCode = Integer.parseInt(errorBean.getFaultCode(),16);
            String errorInfo = "";
            int j = 0;
            for (int i = 0; i < errorCodeArr.length; i ++){

                int errorCodeA = errorCode & errorCodeArr[i];
                if (!"".equals(getErrorInfo(errorCodeA))){

                    Log.e("mainVBNz","errorCodeA = " + errorCodeA + " getErrorInfo = " + getErrorInfo(errorCodeA) + " isExist = " + isExist(getErrorInfo(errorCodeA)));
                    if (!isExist(getErrorInfo(errorCodeA))){

                        existError.add(getErrorInfo(errorCodeA));
                        j ++;
                        errorInfo = errorInfo + j + "、" + getErrorInfo(errorCodeA) + "\n";
                    }
                }
            }
            Log.e("mainVBNz","----------------------------------" + errorInfo);
            existError.clear();
            holder.tv_error.setText(errorInfo);

        }catch (Exception e){

        }
        holder.tv_time.setText(errorBean.getCreateDate());
        return convertView;
    }


    class ViewHolder{

        private TextView tv_time;
        private TextView tv_error;
        private LinearLayout layout;
    }

    private int[] errorCodeArr = {0x01,0x02,0x04,0x08,0x10,0x20,0x40};
    private List<String> existError = new ArrayList();
    private boolean isExist(String value){

        if (existError.contains(value)){

            return true;
        }else{

            return false;
        }
    }
    /**
     * 获取错误信息
     * @param code
     * @return
     */
    private String getErrorInfo(int code){

        String error = "";
        if (code == 0x01){
            error = "电机过流或MOS管突然击穿";
        }
        if (code == 0x02){
            error = "电机堵转";
        }
        if (code == 0x04){
            error = "电机桥臂故障或驱动电路异常";
        }
        if (code == 0x08){
            error = "电机霍尔异常_08";
        }
        if (code == 0x10){
            error = "陀螺仪异常";
        }
        if (code == 0x20){
            error = "24C02异常";
        }
        if (code == 0x40){
            error = "电机霍尔异常_40";
        }
        return error;
    }

}
